﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using AssetBundleHelper;

namespace HotUI{
	public class UIMgr:SingleClass<UIMgr>{
		private FSM uifsm;
		public bool isInited{
			get{
				return uifsm != null && UIViewMgr.Instance.isInited;
			}
		}
		Dictionary<string,State> statePaths = new Dictionary<string, State> ();
		Dictionary<string,StateMachine> stateMachinePath = new Dictionary<string, StateMachine> ();
		List<LuaView> dialogs = new List<LuaView>();
		public UIMgr(){
			InitFSM ();
			UIViewMgr.Instance.Begin ();
		}
		void InitFSM(){
			GameTool.LoadJSON ("UIFSM", delegate(ABObject obj) {
				uifsm = new FSM ();
				var stateMachineNode = obj.jsonNode["stateMachine"];
				for(var i = 0;i < stateMachineNode.Count;i++){
					var sp = new StatePath(stateMachineNode[i]["path"].ToString());
					var sm = uifsm.AddStateMachine(sp,OnStateMachineEnter,OnStateMachineUpdate,OnStateMachineLeave);
					stateMachinePath.Add(stateMachineNode[i]["name"].ToString(),sm);
				}
				var statenode = obj.jsonNode["state"];
				for(var i = 0;i < statenode.Count;i++){
					var sp = new StatePath(statenode[i]["path"].ToString());
					var st = uifsm.AddState(sp,OnStateEnter,OnStateUpdate,OnStateLeave);
					statePaths.Add(statenode[i]["name"].ToString(),st);
				}
			});
		}
		void OnStateMachineEnter(){
			if (uifsm.LastState == null || (uifsm.LastState.parent.parent.StatePathName != uifsm.state.parent.StatePathName)) {
				OpenView (uifsm.state.parent.StatePathName, uifsm.state.parent.depth,uifsm.state.parent.obj);
			}
		}
		void OnStateMachineUpdate(float dt){
		}
		void OnStateMachineLeave(){
			if (uifsm.LastState == null || (uifsm.LastState.parent.StatePathName != uifsm.state.parent.parent.StatePathName)) {
				CloseView (uifsm.LastState.parent.StatePathName);
			}
		}

		void OnStateEnter(){
			var name = uifsm.state.statePath.StateName == "base" ? uifsm.state.parent.StatePathName : uifsm.state.statePath.StateName;
			OpenView (name, uifsm.state.depth,uifsm.state.obj);
		}
		void OnStateUpdate(float dt){
		}
		void OnStateLeave(){
			var name = uifsm.LastState.statePath.StateName == "base" ? uifsm.LastState.parent.StatePathName : uifsm.LastState.statePath.StateName;
			CloseView(name);
		}
		public LuaView SetState(string name,object arg){
			if (uifsm == null) {
				Debug.Log ("尚未初始化完毕");
				return null;
			}
			if(name == null || name == "")
			{
				Debug.Log("拒绝:["+(name == null?"空":name)+"]状态");
				return null;
			}
			if (stateMachinePath.ContainsKey (name)){
				stateMachinePath[name].obj = arg;
				uifsm.SetState (stateMachinePath [name]);
			}
			else if(statePaths.ContainsKey (name)){
				statePaths[name].obj = arg;
				uifsm.SetState (statePaths [name]);
			}
			else
				Debug.Log (string.Format("找不到状态[{0}]", name));
			return UIViewMgr.Instance.getView (name);
		}
		public void Init(){
			
		}

		public LuaView SetDialogState(string name,object arg){
			var hasView = TryToTop(name);
			if(hasView == -1){
				var luaview = OpenView(name,getDialogDepth(dialogs.Count),arg);
				dialogs.Add(luaview);
				return luaview;
			}else{
				return OpenView(name,hasView,arg);
			}
		}
		public void CloseAllDialog(){
			for(var i = dialogs.Count - 1;i>=0;i--){
				dialogs[i].Close();
			}
		}
		public void CloseDialog(){
			if(dialogs!=null)
				dialogs[dialogs.Count-1].Close();
		}
		int getDialogDepth(int index){
			return 100+index*10;
		}
		int TryToTop(string name){
			LuaView target =  null;
			int id = 0;
			for(var i =0 ;i < dialogs.Count;i++){
				if(dialogs[i].viewName == name){
					target = dialogs[i];
					id = i;
					break;
				}
			}
			if(target != null){
				dialogs.RemoveAt(id);
			
				for(var i = id;i < dialogs.Count;i++){
					dialogs[i].TryUpdateDepth(getDialogDepth(i));
				}

				dialogs.Add(target);
				return getDialogDepth(dialogs.Count-1);
			}
			return -1;
		}

		LuaView OpenView(string name,int depth,object data){
			depth = depth * 10;
			Debug.Log ("打开界面[" + name + "] 深度:" + depth);
			return UIViewMgr.Instance.OpenView (name, depth,data);
		}

		void CloseView(string name){
			Debug.Log ("关闭界面[" + name + "]");
			UIViewMgr.Instance.CloseView (name);
		}
	}
}